<template>
  <div id="content">
    <el-form ref="form" :model="formData" label-width="80px">
      <el-form-item label="菜单名称">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>

      <el-form-item label="菜单路径">
        <el-input v-model="formData.href"></el-input>
      </el-form-item>

      <el-form-item label="上级菜单">
        <el-select v-model="formData.parentId" placeholder="请选择活动区域">
          <el-option label="顶层菜单" :value="-1"></el-option>
          <el-option
            v-for="item in loadMenusData"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="描述">
        <el-input v-model="formData.describe"></el-input>
      </el-form-item>

      <el-form-item label="图标名称">
        <el-input v-model="formData.icon"></el-input>
      </el-form-item>

      <el-form-item label="是否显示">
        <el-radio-group v-model="formData.shown">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createMenusItem, getAllMenus, getEditMenuInfo } from "@/api/menu";
export default {
  name: "AddMenusItem",
  data() {
    return {
      // true为添加，false编辑
      isCreate: undefined,
      formData: {
        id: undefined,
        parentId: -1,
        name: "",
        href: "",
        icon: "",
        orderNum: 0,
        describe: "",
        shown: true,
      },
      loadMenusData: null,
    };
  },
  created() {
    this.loadMenus(); // 获取顶级菜单
    this.isCreate = !this.$route.params.id;
    this.loadEditMenuInfo(); // 获取要编辑的菜单信息
  },
  computed: {
    // 判断是添加还是编辑（true为添加，false编辑）
    msg() {
      return this.isCreate ? "添加" : "编辑";
    },
  },
  methods: {
    onSubmit() {
      createMenusItem(this.formData).then((res) => {
        if (res.data.data) {
          this.$message.success(`${this.msg}菜单成功！`);
          this.$router.push({
            name: "menu",
          });
        } else {
          this.$message.error(`${this.msg}菜单失败！`);
        }
      });
    },
    // 过滤出所有(管理)
    loadMenus() {
      getAllMenus().then((res) => {
        if (res.data.code === "000000") {
          this.loadMenusData = res.data.data.filter((item) => item.level === 0);
        } else {
          this.$message.error("创建菜单时，获取所有菜单信息失败！");
        }
      });
    },
    // 加载获取编辑菜单信息
    loadEditMenuInfo() {
      const { id = -1 } = this.$route.params;
      console.log("id------:", id);
      getEditMenuInfo(id).then((res) => {
        if (res.data.code === "000000") {
          // 根据id获取当前的菜单列表数据
          const { menuInfo } = res.data.data;
          // 将原有的对象和menuInfo合并
          Object.assign(this.formData, menuInfo);
        } else {
          this.$message.error("获取编辑菜单信息失败！");
        }
        console.log(res);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-form {
  padding: 40px;
}
</style>
